package com.mooc.yohoshop.ui.frame;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.blankj.utilcode.util.BarUtils;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.ui.category.CategoryFragment;
import com.mooc.yohoshop.ui.home.HomeFragment;
import com.mooc.yohoshop.ui.seckill.SecKillActivity;
import com.mooc.yohoshop.ui.user.UserFragment;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private UserFragment userFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BarUtils.setStatusBarLightMode(this,false);
        BarUtils.setStatusBarColor(this, Color.BLACK);
        init();
        selFragment(R.id.main_home_radio);
    }

    private void init(){
        ((RadioButton)findViewById(R.id.main_home_radio)).setOnCheckedChangeListener(this::onCheckedChanged);
        ((RadioButton)findViewById(R.id.main_category_radio)).setOnCheckedChangeListener(this::onCheckedChanged);
        ((RadioButton)findViewById(R.id.main_see_radio)).setOnCheckedChangeListener(this::onCheckedChanged);
        ((RadioButton)findViewById(R.id.main_user_radio)).setOnCheckedChangeListener(this::onCheckedChanged);
        findViewById(R.id.main_ufo_img).setOnClickListener(this::onClick);
    }

    private void hideFragment(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (homeFragment != null){
            ft.hide(homeFragment);
        }
        if(categoryFragment!= null){
            ft.hide(categoryFragment);
        }
        if (userFragment!=null)
            ft.hide(userFragment);
        ft.commit();
    }

    private void selFragment(int id){
        hideFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (id == R.id.main_home_radio) {
            if (homeFragment != null) {
                ft.show(homeFragment);
            } else {
                homeFragment = new HomeFragment();
                ft.add(R.id.main_fm_relative,homeFragment);
            }
        }if (id == R.id.main_category_radio){
            if (categoryFragment != null)
                ft.show(categoryFragment);
            else {
                categoryFragment = new CategoryFragment();
                ft.add(R.id.main_fm_relative,categoryFragment);
            }
        }if (id == R.id.main_user_radio){
            if (userFragment!=null)
                ft.show(userFragment);
            else {
                userFragment = new UserFragment();
                ft.add(R.id.main_fm_relative,userFragment);
            }
        }
        ft.commit();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!isChecked)
            return;
        selFragment(buttonView.getId());
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(this, SecKillActivity.class));
    }
}